package com.cheboohui.iqs.spider.pingan.price;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 汽车计算实际价值
 * 
 * @author xielonghai
 *
 */
public class ComputeCar {
	/*** 折旧率 ***/
	private static double depreciationRate = 0.006;

	/* 车辆折旧价格 */
	public static double compute(double price, String time, String timeEnd) {
		int months = getMonthSpace(time, timeEnd);// 获得时间差 (月数)
		return price * (1 - depreciationRate * months);// 折旧计算公式------------
	}
	
	/* 车辆折旧价格 */
	public static String compute(String price, String time, String timeEnd) {
		double prices = Double.parseDouble(price);
		int months = getMonthSpace(time, timeEnd);// 获得时间差 (月数)
		double actualPrice = prices * (1 - depreciationRate * months);// 折旧计算公式------------
		return stringFormat(String.valueOf(actualPrice));
	}

	/**
	 * <计算来两个时间段间的月数> <功能详细描述>
	 * 
	 * @param date1
	 *            开始时间
	 * @param date2
	 *            结束时间
	 * @return
	 * @throws ParseException
	 * @see [类、类#方法、类#成员]
	 */
	public static int getMonthSpace(String date1, String date2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(sdf.parse(date1));
			c2.setTime(sdf.parse(date2));
		} catch (Exception e) {
			e.printStackTrace();
		}

		int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
		int month = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);

		if (c2.get(Calendar.DAY_OF_MONTH) + 1 < c1.get(Calendar.DAY_OF_MONTH)) {
			month = month - 1;
		}

		if (year < 0) {
			return 0;
		} else if (year > 0) {
			return 12 * year + month;
		} else {
			if (month > 0)
				return month;
			else if (month < 0)
				return 0;
			else
				return 1;
		}
	}

	public static String stringFormat(String str) {
		DecimalFormat df = new DecimalFormat();
		String style = "0000.00#";
		df.applyPattern(style);
		return df.format((Double.parseDouble(str)));
	}
}
